// package com.yhl.login;
//
// /**
//  * @author: yanghl
//  * @description: TODO
//  * @date: 2024/7/15 17:31
//  */
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
// import org.springframework.security.core.context.SecurityContextHolder;
// import org.springframework.security.core.userdetails.UserDetails;
// import org.springframework.security.core.userdetails.UserDetailsService;
// import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
// import org.springframework.stereotype.Component;
// import org.springframework.web.filter.OncePerRequestFilter;
//
// import javax.servlet.FilterChain;
// import javax.servlet.ServletException;
// import javax.servlet.http.HttpServletRequest;
// import javax.servlet.http.HttpServletResponse;
// import java.io.IOException;
//
// @Component
// public class JwtFilter extends OncePerRequestFilter {
//
//     @Autowired
//     private JwtUtil jwtUtil;
//
//     @Autowired
//     private UserDetailsService userDetailsService;
//
//     @Override
//     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
//             throws ServletException, IOException {
//
//         String authorizationHeader = request.getHeader("Authorization");
//
//         String token = null;
//         String username = null;
//
//         if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
//             token = authorizationHeader.substring(7);
//             username = jwtUtil.extractClaims(token).getSubject();
//         }
//
//         if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
//             UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//
//             if (jwtUtil.validateToken(token, userDetails.getUsername())) {
//                 UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
//                         new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
//                 SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
//             }
//         }
//
//         filterChain.doFilter(request, response);
//     }
// }
//
